"""

"""

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.filechooser import FileChooserListView
from pathlib import Path

class FileChooser(BoxLayout):
    """"""
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.orientation = 'vertical'
        self.spacing = 10
        self.padding = 20
        
        self.selected_file = None
        self.callback = None
        
        self._build_ui()
    
    def _build_ui(self):
        """"""
        # 
        title = Label(
            text="Select Video File",
            size_hint_y=None,
            height=60,
            font_size=36  # 放大两倍字体
        )
        self.add_widget(title)
        
        # 
        self.file_chooser = FileChooserListView(
            filters=['*.mp4', '*.avi', '*.mkv', '*.mov', '*.wmv', 
                    '*.mp3', '*.wav', '*.flac', '*.aac', '*.ogg'],
            path=str(Path.home())
        )
        self.add_widget(self.file_chooser)
        
        # 
        button_layout = BoxLayout(
            orientation='horizontal',
            size_hint_y=None,
            height=50,
            spacing=10
        )
        
        # 
        select_btn = Button(
            text="Select File",
            size_hint_x=0.5,
            font_size=24  # 放大字体
        )
        select_btn.bind(on_press=self._on_select)
        button_layout.add_widget(select_btn)
        
        # 
        cancel_btn = Button(
            text="Cancel",
            size_hint_x=0.5,
            font_size=24  # 放大字体
        )
        cancel_btn.bind(on_press=self._on_cancel)
        button_layout.add_widget(cancel_btn)
        
        self.add_widget(button_layout)
    
    def _on_select(self, instance):
        """"""
        if self.file_chooser.selection:
            self.selected_file = self.file_chooser.selection[0]
            if self.callback:
                self.callback(self.selected_file)
    
    def _on_cancel(self, instance):
        """"""
        if self.callback:
            self.callback(None)
    
    def set_callback(self, callback):
        """"""
        self.callback = callback